package com.sxhalo.fsms.controller;


import com.sxhalo.fsms.domain.SysDicCode;
import com.sxhalo.fsms.service.IDeviceAlarmRecordService;
import com.sxhalo.fsms.service.IFactDeviceService;
import com.sxhalo.fsms.service.ISysDicCodeService;
import com.sxhalo.fsms.service.IUserPatrolRecordService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
public class AntProController  implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping(value = "/error")
    public String getIndex() {
        return "index";
    }

    @Autowired
    private ISysDicCodeService codeService;
    @Autowired
    private IFactDeviceService deviceService;
    @Autowired
    private IDeviceAlarmRecordService alarmRecordService;
    @Autowired
    private IUserPatrolRecordService patrolRecordService;

    //登录后获取字典数据用于本地存储
    @ResponseBody
    @RequestMapping(value = "/api/dictdata", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
    public ApiResult findDic(HttpServletRequest request) {
        List<SysDicCode> list = codeService.findWhereList("delete_flag=0", "type_id ,dict_code");
        Map<Object,Object> dicMap=new HashMap<Object,Object>();
        List<SysDicCode>  dicList=new ArrayList<SysDicCode>();

        int type=0;
        Map<String,String>  dicEnum=new HashMap<String,String>();
        for(SysDicCode dic:list){
            if(type!=dic.getTypeId()){
               if(type!=0){
                   dicMap.put(type+"Enum",dicEnum);
                   dicMap.put(type,dicList);
               }
                dicEnum=new HashMap<String,String>();
                dicList=new ArrayList<SysDicCode>();
                type=dic.getTypeId();
            }
            dicEnum.put(dic.getDictCode(),dic.getDictValue());
            dicList.add(dic);

        }
        dicMap.put(type,dicList);
        dicMap.put(type+"Enum",dicEnum);
        return ApiResult.retOK(request,dicMap);
    }



    // 首页统计
    @ResponseBody
    @RequestMapping(value = "/api/home/statistics", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public ApiResult getDevicCount(Integer areaId, HttpServletRequest req) {
        long time1=System.currentTimeMillis();

        Map<String,Object> returnData=new HashMap<String,Object>();

        //设备数统计
        Map<String, Object> statistics = deviceService.getStatistics(areaId == null ? " use_state=1 "
                : "  use_state=1  and   FIND_IN_SET(area_id,fun_get_area_chindids(" + areaId + "))  ");

        //报警总数
        List<Map<String,Integer>>  todayAlarmTypeCount= alarmRecordService.getAramTypeNum("  alarm_time >  CURDATE()"+(areaId == null ? ""
                : " and  device_id in (select device_id from  fact_device where delete_flag=0 and  FIND_IN_SET(area_id,fun_get_area_chindids(" + areaId
                + ")) )  "));
        //当日报警
        List<Map<String,Integer>>  alarmTypeCount= alarmRecordService.getAramTypeNum((areaId == null ? ""
                : "   device_id in (select device_id from  fact_device where delete_flag=0 and  FIND_IN_SET(area_id,fun_get_area_chindids(" + areaId
                + ")) )  "));


//        Integer deveciNum=deviceService.getChildCount(" delete_flag=0 and use_state=1 ",areaId);
        //巡检统计
        Map<String, Object> patrolRecord=patrolRecordService.getRecentRecord(areaId == null ? ""
                : "   FIND_IN_SET(area_id,fun_get_area_chindids(" + areaId + "))  ");


        //运行设备
        List<Map<String, Integer>> runstateCount=alarmRecordService.getDeviceRunstateCount(areaId);


//        runstateCount.put("deveciNum",deveciNum);
        returnData.put("deviceCount",statistics);
        returnData.put("alarmTypeCount",alarmTypeCount);
        returnData.put("todayAlarmTypeCount",todayAlarmTypeCount);

        returnData.put("patrolRecord",patrolRecord);
        returnData.put("runstateCount",runstateCount);
       
        return ApiResult.retOK(req, returnData);
    }
}